//
//  RecoummendHotelDataVC.m
//  AroundTheWorld
//
//  Created by IfelseGo on 16/5/24.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "RecoummendHotelDataVC.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "DataHandler.h"
#import "RecommendHotelDataCityModel.h"
#import "RecommendHotelDataCityTableCell.h"
#import "RecommendHotelDataHotCityTableCell.h"

static NSString *const hotelReuse = @"subjectReuse";
static NSString *const hotelABCReuse = @"hotelABCReuse";
static NSString *const hotelHotReuse = @"hotelHotReuse";

@interface RecoummendHotelDataVC ()
@property(nonatomic, strong) UITableView *myTableV;
@end

@implementation RecoummendHotelDataVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
    
    self.myTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.myTableV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableV];
    self.myTableV.dataSource = self;
    self.myTableV.delegate = self;
    
    //样式
    self.myTableV.separatorColor = [UIColor grayColor];
    self.myTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableV.dataSource = self;
    //注册
    [self.myTableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellReuse"];
    UINib *nib = [UINib nibWithNibName:@"RecommendHotelDataCityTableCell" bundle:[NSBundle mainBundle]];
    [self.myTableV registerNib:nib forCellReuseIdentifier:hotelABCReuse];
    [self.myTableV registerClass:[RecommendHotelDataHotCityTableCell class] forCellReuseIdentifier:hotelHotReuse];

    
    // Do any additional setup after loading the view.
}
//data
-(void)getData{
    self.myHotelCityArrayM = [NSMutableArray array];
    self.myHotelCityDicM = [NSMutableDictionary dictionary];
    self.myHotelCityAllKeyArrayM = [NSMutableArray array];
    
    DataHandler *handler1 = [[DataHandler alloc] init];
    [handler1 getRecoummendHotelDataCityNotes:^(id result) {
        self.myHotelCityArrayM = [NSMutableArray arrayWithArray:result];
        //allKeyArray
        for (RecommendHotelDataCityModel *modelTemp in self.myHotelCityArrayM) {
            if (modelTemp.is_hot == YES) {
                if ([[self.myHotelCityDicM allKeys] containsObject:@" 热门"]) {
                    NSMutableArray *arrayTemp = [self.myHotelCityDicM objectForKey:@" 热门"];
                    [arrayTemp addObject:modelTemp];
                }else{
                    NSMutableArray *array1 = [NSMutableArray array];
                    [array1 addObject:modelTemp];
                    [self.myHotelCityDicM setObject:array1 forKey:@" 热门"];
                }
            }else{
                NSString *key = [modelTemp.pinyin substringToIndex:1];
                if ([[self.myHotelCityDicM allKeys] containsObject:key]) {
                    NSMutableArray *arrayTemp = [self.myHotelCityDicM objectForKey:key];
                    [arrayTemp addObject:modelTemp];
                }else{
                    NSMutableArray *array1 = [NSMutableArray array];
                    [array1 addObject:modelTemp];
                    [self.myHotelCityDicM setValue:array1 forKey:key];
                }
            }
        }
        self.myHotelCityAllKeyArrayM =[NSMutableArray arrayWithArray:self.myHotelCityDicM.allKeys];
        [self.myHotelCityAllKeyArrayM sortUsingSelector:@selector(compare:)];
        [self.myTableV reloadData];
    } failedHandler:^(NSError *error) {
    }];
}

// 分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.myHotelCityAllKeyArrayM.count;
}

// 分区包含多少个元素
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = [self.myHotelCityAllKeyArrayM objectAtIndex:section];
    if (section == 0) {
        return 1;
    }
    return [[self.myHotelCityDicM objectForKey:key] count];
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 205;
    }
    return 65;
}
//显示分区名
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *sectonName = [self.myHotelCityAllKeyArrayM objectAtIndex:section];
    return sectonName;
}
//建cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSUInteger sectionNo = indexPath.section;
    NSUInteger rowNo = indexPath.row;
    if (sectionNo == 0) {
        RecommendHotelDataHotCityTableCell *cell = [tableView dequeueReusableCellWithIdentifier:hotelHotReuse];
        NSMutableArray *array = [self.myHotelCityDicM objectForKey:@" 热门"];
        cell.myArrayM = array;
        return cell;
    }
    NSString *key = [self.myHotelCityAllKeyArrayM objectAtIndex:sectionNo];
    RecommendHotelDataCityTableCell *cell = [tableView dequeueReusableCellWithIdentifier:hotelABCReuse];
    NSArray *arrayTp = [self.myHotelCityDicM objectForKey:key];
    RecommendHotelDataCityModel *model = [arrayTp objectAtIndex:rowNo];
    cell.model  = model;
     return cell;
}
//cell触发方法
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    DataViewController *dataVC = [[DataViewController alloc] init];
//    // 获取分区号
//    NSUInteger sectionNo = indexPath.section;
//    // 获取表格行的行号
//    NSUInteger rowNo = indexPath.row;
//    NSString *key = [self.arrayKey objectAtIndex:sectionNo];
//    NSArray *array = [self.myBigDic objectForKey:key];
//    dataVC.model = [array objectAtIndex:rowNo];
//    
//    [self.navigationController pushViewController:dataVC animated:YES];
//}

//右侧显示数组
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.myHotelCityAllKeyArrayM;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
